package com.StarStudio.Service.Impl;

import cn.hutool.core.util.IdUtil;
import com.StarStudio.DAO.ComplaintDAO;
import com.StarStudio.DAO.UserInfoDAO;
import com.StarStudio.Service.ComplaintService;
import com.ruoyi.common.core.Entity.Complaint;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    ComplaintDAO complaintDAO;

    @Autowired
    UserInfoDAO userInfoDAO;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertComplaint(Complaint complaint) {
        complaint.setId(IdUtil.getSnowflakeNextId());
        if (complaint.getComplaintPicture() != null){
            complaint.setComplaintPicture(complaint.getComplaintPicture().toString());
        }
        complaint.setCreateTime(new Date());
        complaint.setDelFlag(false);
        complaintDAO.insertComplaint(complaint);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateComplaint(Complaint complaint) {
        if (complaint.getComplaintPicture() != null){
            complaint.setComplaintPicture(complaint.getComplaintPicture().toString());
        }
        complaint.setUpdateTime(new Date());
        complaintDAO.updateComplaint(complaint);
    }


    @Override
    public List<Complaint> selectComplaint(Complaint complaint) {
        if (complaint.getComplaintPicture() != null){
            complaint.setComplaintPicture(complaint.getComplaintPicture().toString());
        }
        List<Complaint> complaintList = complaintDAO.selectComplaint(complaint);
        if (complaintList != null && complaintList.size() > 0){
            for (Complaint complaint1 : complaintList) {
                //处理返回的图片URL数组
                String s = complaint1.getComplaintPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                complaint1.setComplaintPicture(stringList);
            }
        }
        return complaintList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteComplaint(Long id) {
        complaintDAO.deleteComplaint(id);
    }

    @Override
    public List<Complaint> selectRecentlyComplaint() {
        List<Complaint> complaintList = complaintDAO.selectRecentlyComplaint();
        if (complaintList != null && complaintList.size() > 0){
            for (Complaint complaint1 : complaintList) {
                //处理返回的图片URL数组
                String s = complaint1.getComplaintPicture().toString().replace("[", "").replace("]", "").replace(" ", "");
                List<String> stringList = new ArrayList<>();
                if (StringUtils.isBlank(s)) {
                    stringList = Collections.emptyList();
                } else if (s.contains(",")) {
                    stringList = Arrays.asList(s.split(","));
                } else {
                    stringList.add(s);
                }
                complaint1.setComplaintPicture(stringList);
            }
        }
        return complaintList;
    }

    @Override
    public long findAllRecordNumber() {
        return complaintDAO.findAllRecordNumber();
    }
}
